Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

213
Vistas
React state hook Object not updating, getting [Object object]

In my project I am having trouble setting a state variable with a javascript object, I can see in my console logs that the object is being instansiated correctly with the right values, but I cannot figure out why it is not properly setting the state hook.

Any help would be appreciate!!

const [accountInfo, setAccountInfo] = useState({})

const delay = ms => new Promise(res => setTimeout(res, ms));

const onLogin = async (account) => {

    await(delay(2000));

    console.log(account); // I am getting the proper output of the object here

    if(account.account_id != -1){

      setAccountInfo(account);

      await delay(4000); //Just for debugging
      console.log("acc " + accountInfo); // This outputs [Object object]
      setLoggedIn(true);
    }else{
      alert('Account does not exist');
    }
  }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are trying to concatenate string with object. Either convert object to string with JSON.stringify or use object.property.

console.log("Object" + JSON.stringify(object_to_print));

you are trying to console.log a state variable. React is not guaranteeing the state change immediately. you can access state change in useEffect hook

useEffect(() => { 
      console.log('state changed', your-state-variable) 
      // write your callback function here
 }, [your-state-variable]);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda